package com.niit.project.utils;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

//这里我们连接数据库的技术时德鲁伊连接池(Druid),而不是我们上学期学习的JDBC
//使用JDBC首先要加载JDBC驱动程序,然后建立数据库连接Connection,
// 创建执行SQL的语句Statement,处理执行结果ResultSet,最后释放资源.
//这种方式每次和数据库连接时都要实例化一下连接数据的方法和类
//而德鲁伊连接池则是提前创建好几个连接,用户使用时从池子中取就可以了,而不需要重新创建
//大大简化了代码
public class Dbutils {
    private static DruidDataSource dataSource;
    private static final ThreadLocal<Connection> THREAD_LOCAL=new ThreadLocal<>();//创建线程

    static {
        //创建Properties对象
        Properties properties = new Properties();
        //将配置文件转换成字节流输入
        InputStream inputStream = Dbutils.class.getResourceAsStream("/database.properties");
        try {
            //使用properties对象加载配置文件信息
            properties.load(inputStream);
            //druid底层使用的是工厂模式创建连接池
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //获取连接方法
    public static Connection getConnection()
    {
        Connection connection = THREAD_LOCAL.get();
        try {
            if(connection==null)
            {
                //通过连接池获得连接对象
                connection = dataSource.getConnection();
                //将获取的连接对象放在连接池中
                THREAD_LOCAL.set(connection);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return connection;
    }

    //控制事务  事务的开启
    public static void begin()
    {
        Connection connection = null;
        try {
            connection = getConnection();
            //设置事务手动提交,setautocommit为true时表示自动提交
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //事务的提交
    public static void commit()
    {
        Connection connection = null;
        try {
            connection = getConnection();
            connection.commit();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            closeAll(connection,null,null);
        }
    }

    //事务的回滚
    public static void rollback()
    {
        Connection connection = null;
        try {
            connection = getConnection();
            connection.rollback();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            closeAll(connection,null,null);
        }
    }

    //连接的关闭
    public static void closeAll(Connection connection, Statement statement, ResultSet resultSet)
    {
        try {
            if(resultSet!=null)
            {
                resultSet.close();
            }
            if(statement!=null)
            {
                statement.close();
            }
            if(connection!=null)
            {
                connection.close();
                THREAD_LOCAL.remove();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }



}
